package com.demo.webmagic.pageProcessor;

import java.util.ArrayList;
import java.util.List;

import com.demo.common.constant.ConstantDtoFlight;
import com.demo.common.model.Proxyip;
import com.demo.common.model._MappingKit;
import com.demo.webmagic.dto.DtoProxyIp;
import com.demo.webmagic.pipeline.PipelineProxyIp;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class PageProcessorHaodaili implements PageProcessor {

	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setCycleRetryTimes(3);
	Log log = Log.getLog(DtoProxyIp.class);
	
	@Override
	public Site getSite() {
		return site;
	}

	@Override
	public void process(Page page) {
		DtoProxyIp dtoProxyIp = new DtoProxyIp();
		try {
			List<Proxyip> proxyIpList = new ArrayList<Proxyip>();
			List<Selectable> trList =  page.getHtml().xpath("//table[@class='proxy_table']/tbody/tr").nodes();
			for (int i = 1; i < trList.size(); i++) {
				Selectable tr = trList.get(i);
				List<Selectable> tdList = tr.xpath("//td/text()").nodes();
				if("高匿".equals(tdList.get(4).toString().trim()) && "HTTP".equals(tdList.get(3).toString().trim())) {
					Proxyip proxyip = new Proxyip();
					proxyip.setIpStr(tdList.get(0).toString().trim());
					proxyip.setPortStr(tdList.get(1).toString().trim());
					proxyip.setLocation(tdList.get(2).toString().trim());
					proxyip.setValiDate(tdList.get(5).toString().trim());
					proxyIpList.add(proxyip);
					for (Selectable td : tdList) {
						System.out.println(td.toString().trim());
					}
					System.out.println("+++++++++++++++++++");
				}
			}
			dtoProxyIp.setCode(ConstantDtoFlight.code_success);
			dtoProxyIp.setProxyIpList(proxyIpList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			dtoProxyIp.setCode(ConstantDtoFlight.code_fail);
		} finally {
			page.putField("result", dtoProxyIp);
		}
	}
}
